package com.cinema.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.alibaba.fastjson.JSON;
import com.cinema.Bean.Hall;
import com.cinema.Bean.Movie;
import com.cinema.Bean.MovieSession;
import com.cinema.Bean.MsgStatistical;
import com.cinema.Bean.User;
import com.cinema.dao.IHallDao;
import com.cinema.dao.IMovieDao;
import com.cinema.dao.IMovieSessionDao;
import com.cinema.dao.ITicketDao;
import com.cinema.dao.IUserDao;
import com.cinema.dao.imp.HallDaoMysql;
import com.cinema.dao.imp.MovieDaoMysql;
import com.cinema.dao.imp.MovieSessionDaoMysql;
import com.cinema.dao.imp.TicketDaoMysql;
import com.cinema.dao.imp.UserDaoMysql;

public class InfoServlet extends HttpServlet {

	IMovieDao iMovieDao = new MovieDaoMysql();
	ITicketDao iTicketDao = new TicketDaoMysql();
	IHallDao iHallDao = new HallDaoMysql();
	IUserDao iUserDao = new UserDaoMysql();
	IMovieSessionDao iMovieSessionDao = new MovieSessionDaoMysql();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String method = request.getParameter("method");
		
		if ("getMovieInfo".equals(method)) { // 场次新增，场厅、电影下拉框设置
			// 所有场厅
			List<Hall> HallList=iHallDao.selectAll();
			request.setAttribute("HallList", HallList);
			
			// 所有电影(除去已经下映影片)
			List<Movie> MovieList=iMovieDao.selectByWhere("movieStat !=3 ");
			request.setAttribute("MovieList", MovieList);
			
			//将电影信息传到场次新增界面
			RequestDispatcher rd=request.getRequestDispatcher("Session/add.jsp");
			rd.forward(request, response);
			
		}else if("getHallInfo".equals(method)) { // 座位新增，场厅下拉框设置
			// 所有场厅
			List<Hall> HallList=iHallDao.selectAll();
			request.setAttribute("HallList", HallList);
			
			/*将场厅信息传到座位新增界面*/
			RequestDispatcher rd=request.getRequestDispatcher("Seats/add.jsp");
			rd.forward(request, response);
			
		}else if("getStatisticalData".equals(method)) { //用户统计
			// 获取所有的用户
			List<MsgStatistical> userInfo = iUserDao.selectByStatWhere();
			request.setAttribute("userInfo", userInfo);
			
			// 获取所有的排片
			List<MsgStatistical> movieSessionInfo = iMovieSessionDao.selectByStatWhere();
			request.setAttribute("movieSessionInfo", movieSessionInfo);
			
			// 获取所有的收入
			List<MsgStatistical> incomeInfo = iTicketDao.selectByStatWhere();
			request.setAttribute("incomeInfo", incomeInfo);
			
			// 获取收入占比
			List<MsgStatistical> incomeCountInfo = iTicketDao.selectByStatAccount();
			Map<Object,Object> map = new HashMap<Object,Object>();
			List<Object> list = new ArrayList<Object>();
			for (int i = 0; i < incomeCountInfo.size(); i++) {
				MsgStatistical m = incomeCountInfo.get(i);
				DecimalFormat df = new DecimalFormat("0.00");  
				Double a = Double.parseDouble(m.getCount()); // 单个热映影片的总收入
				Double b = Double.parseDouble(m.getTime()); // 全部影片的总收入
				String ab =  df.format(a/b); // 占比
				Double c = Double.parseDouble(ab);
				map.put("name",m.getMovieName());
				map.put("y",c);
				String json = JSON.toJSONString(map);
				list.add(json);
			}
			request.setAttribute("incomeCountInfo", list);
			
			// 获取所有的影片
			List<MsgStatistical> movieInfo = iMovieDao.selectByStatWhere();
			request.setAttribute("movieInfo", movieInfo);
			
			/*将场厅信息传到座位新增界面*/
			RequestDispatcher rd=request.getRequestDispatcher("Statistics/userStatistics.jsp");
			rd.forward(request, response);
		}
	
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
